package com.wzx.user.business.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzx.user.pojo.shop.pojo.ShopAddress;
import com.wzx.user.business.shop.mapper.ShopAddressMapper;
import com.wzx.user.business.shop.service.ShopAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wzx
 * @version 1.0
 * @date 2022/9/7 - 14:42
 * @description 标题
 */
@Service
@Transactional
public class ShopAddressServiceImpl extends ServiceImpl<ShopAddressMapper, ShopAddress> implements ShopAddressService {

    @Autowired
    private ShopAddressMapper shopAddressMapper;

    @Override
    public void addAddress(ShopAddress shopAddress) {
        //判断是否设为默认，如果是设为默认则将其他的默认改为非默认
        if (shopAddress.getIsDefault().equals("1")){
            shopAddressMapper.updateIsDefault(shopAddress.getUserId());

        }
        shopAddressMapper.insert(shopAddress);
    }

    @Override
    public void updateAddress(Integer addressId, Integer userId) {
        //1，先将所有的用户的状态改为 ‘0’
        shopAddressMapper.updateIsDefault(userId);
        //2，将指定的地址状态改为默认
        shopAddressMapper.updateIsDefaultTrue(addressId);

    }

    @Override
    public void updateAddressObject(ShopAddress shopAddress) {
        //判断是否设为默认，如果是设为默认则将其他的默认改为非默认
        if (shopAddress.getIsDefault().equals("1")){
            shopAddressMapper.updateIsDefault(shopAddress.getUserId());
        }
        shopAddressMapper.updateById(shopAddress);
    }

    @Override
    public List<ShopAddress> getShopAddress(ShopAddress shopAddress) {
        return shopAddressMapper.getShopAddress(shopAddress);
    }
}
